package com.xyzisme76.application;

import com.xyzisme76.dao.StudentDao;
import com.xyzisme76.pojo.Course;
import com.xyzisme76.pojo.Sc;
import com.xyzisme76.pojo.Student;
import com.xyzisme76.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Statistical {
    private static final int WIDTH =860;
    private static final int HEIGHT =600;
    Statistical(){
        sqlSelectInfo();
    }

    void sqlSelectInfo() {
        JFrame jf = new JFrame("平均分");
        jf.setSize(new Dimension(WIDTH,HEIGHT));
        //jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 创建内容面板，使用边界布局
        //JPanel panel = new JPanel(new BorderLayout());
        // 所在位置 居中
        jf.setLocationRelativeTo(null);
        //设置背景图片
        JPanel jp = new JPanel(null) {
            protected void paintComponent(Graphics g) {
                try {
                    Image bg = ImageIO.read(new File(
                            "src/main/java/com/xyzisme76/application/images/averegeback.png"));
                    g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
        };
        //初始化显示学生信息表，后期会刷新表单

        // 表头（列名）


        List<Course> courses = CourseInfo.CourseGet();
        List<Student> students=StudentInfo.studentsGet();
        int forNut = courses.size();
        int forNut2 = students.size();
        int i=0;
        ArrayList<Object> temp = new ArrayList<Object>();
        temp.add("学号");
        temp.add("姓名");
        while(i<forNut){
            temp.add(courses.get(i).getCname());
            i++;
        }
        temp.add("平均分");
        temp.add("排名");
        Object[][] rowData = new Object[forNut2][4+forNut];// 表格所有行数据
        int j = 0,r=1;
        List<String> rank=ScInfo.getRank();
        for(String rank1:rank){
            System.out.println(rank1);
        }
        for(String rank1:rank) {
            System.out.println(rank1);
            rowData[j][0] = rank1;
            rowData[j][1] = StudentInfo.stuNameGet(rank1);
            int k=0;
            int sum=0;
            for(;k<forNut;k++){
                System.out.println(courses.get(k).getCno());
                int re=ScInfo.getGrade(new Sc(rank1,courses.get(k).getCno()));
                rowData[j][2+k]=re;
                sum+=re;
            }
            rowData[j][2+forNut] = 1.0*sum/forNut;
            rowData[j][2+forNut+1] = r;
            r++;
            j++;
        }
        // 创建 表格模型，指定 所有行数据 和 表头
        TableModel tableModel = new DefaultTableModel(rowData, temp.toArray());

        // 使用 表格模型 创建 表格
        JTable table = new JTable(tableModel);
        //JButton jFail = new JButton("统计不及格人数");
        //JButton jSc = new JButton("按班级统计分数段");
        //JButton jTittle = new JButton("各科平均分");
        JButton jSort = new JButton("学生平均分排名");
        //JTextArea information = new JTextArea("信息");

        //jp.add(jTittle);
        //jp.add(jStudent);
        //jp.add(jFail);
        //jp.add(jSc);

        jp.add(jSort);
        jp.add(table.getTableHeader());
        jp.add(table);

        jp.setLayout(null);
        jSort.setForeground(Color.DARK_GRAY);
        jSort.setFont(new Font("微软雅黑", Font.BOLD, 33));
        //布局太麻烦了，直接绝对位置
        jSort.setBackground(Color.white);
        jSort.setMargin(new Insets(0,0,0,0));//将边框外的上下左右空间设置为0
        jSort.setBorderPainted(false);//不打印边框
        jSort.setFocusPainted(false);//除去焦点的框
        jSort.setContentAreaFilled(false);//除去默认的背景填充
        jSort.setBounds(250,32,300,60);
        table.setBounds(120, 150, 600, 600);
        table.getTableHeader().setBounds(120,120,600,30);
        //jTittle.setBounds(100,10,190,60);
        //jFail.setBounds(300,10,250,60);
        //jSc.setBounds(600,10,250,60);
        //information.setBounds(100,70,300,300);
        jf.setContentPane(jp);


        jf.setLocationRelativeTo(null);
        jf.setVisible(true);

    }
}

